package com.ericsson.eritask.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ericsson.eritask.dao.ShowTaskDao;
import com.ericsson.eritask.domain.Project;
import com.ericsson.eritask.domain.Task;
import com.ericsson.eritask.dto.MyTaskDTO;
import com.ericsson.eritask.dto.ProjectDTO;

@Repository
public class ShowTaskDaoImpl implements ShowTaskDao {
	
	@PersistenceContext
	EntityManager entityManager;
	
	@Transactional
	@Override
	public List<MyTaskDTO> showMyTaskList(Integer projId)
	{
		List<MyTaskDTO> myTaskList = new ArrayList<MyTaskDTO>();
		List<Task> listOfTasks = new ArrayList<Task>();
		 Project p = entityManager.find(Project.class, projId);
		 if(p!=null){
			 System.out.println("project_name :"+p.getProjectName()); 
			 listOfTasks = p.getTasks();
			// System.out.println("project_taskSubject :"+p.getTasks().get(0).getSubject()); 
		 }
		
		if(listOfTasks!=null && listOfTasks.size()>0){
		for(Task t : listOfTasks)
		{
			MyTaskDTO myTaskDTO = new MyTaskDTO();
			myTaskDTO.setProjectId(p.getProjectId());
			myTaskDTO.setProjectName(p.getProjectName());
			myTaskDTO.setTaskId(t.getTaskId());
			myTaskDTO.setSubject(t.getSubject());
			myTaskDTO.setStartDate(t.getStartDate());
			myTaskDTO.setPriority(t.getPriority());
			myTaskDTO.setStatus(t.getStatus());
			myTaskDTO.setDueDate(t.getDueDate());
			myTaskDTO.setPercentageComplete(t.getPercentageComplete());
			myTaskList.add(myTaskDTO);
			if(myTaskList!=null){
			System.out.println("myTaskList:"+myTaskList.size());
			}
		}
		}
		for(MyTaskDTO m : myTaskList)
		{
			System.out.println("-----------------"+m.getSubject());
			System.out.println("-----------------"+m.getProjectName());
			System.out.println("-----------------"+m.getPercentageComplete());
			System.out.println("-----------------"+m.getTaskId());
		}
		return myTaskList;
	}

	
	@Transactional
	@Override
	public List<MyTaskDTO> showMyUserTaskList(Integer projId)
	{
		List<MyTaskDTO> myTaskList = new ArrayList<MyTaskDTO>();
		List<Task> listOfTasks = new ArrayList<Task>();
		 Project p = entityManager.find(Project.class, projId);
		 if(p!=null){
			 System.out.println("project_name :"+p.getProjectName()); 
			 listOfTasks = p.getTasks();
			// System.out.println("project_taskSubject :"+p.getTasks().get(0).getSubject()); 
		 }
		
		if(listOfTasks!=null && listOfTasks.size()>0){
		for(Task t : listOfTasks)
		{
			MyTaskDTO myTaskDTO = new MyTaskDTO();
			myTaskDTO.setProjectId(p.getProjectId());
			myTaskDTO.setProjectName(p.getProjectName());
			myTaskDTO.setTaskId(t.getTaskId());
			myTaskDTO.setSubject(t.getSubject());
			myTaskDTO.setStartDate(t.getStartDate());
			myTaskDTO.setPriority(t.getPriority());
			myTaskDTO.setStatus(t.getStatus());
			myTaskDTO.setDueDate(t.getDueDate());
			myTaskDTO.setPercentageComplete(t.getPercentageComplete());
			myTaskList.add(myTaskDTO);
			if(myTaskList!=null){
			System.out.println("myTaskList:"+myTaskList.size());
			}
		}
		}
		for(MyTaskDTO m : myTaskList)
		{
			System.out.println("-----------------"+m.getSubject());
			System.out.println("-----------------"+m.getProjectName());
			System.out.println("-----------------"+m.getPercentageComplete());
			System.out.println("-----------------"+m.getTaskId());
		}
		return myTaskList;
	}

	
}
